#include "Logger.h"
#include "Assert.h"
#include <iostream>

namespace utility
{
    
    Logger::Logger()
    {}
    
    Logger* Logger::instance_ = 0;
    
    Logger* Logger::getLogger()
    {
        if (instance_ == 0) instance_ = new Logger;
        return instance_;
    }
    
    bool Logger::isDebugEnabled() const
    {
        return true;
    }
    
    const char* logLevelEnumToString(LogLevelEnum m)
    {
        switch (m)
        {
            case DEBUG_LOG_LEVEL:
                return "DEBUG";
            case INFO_LOG_LEVEL:
                return "INFO";
            case WARN_LOG_LEVEL:
                return "WARN";
            case NOTICE_LOG_LEVEL:
                return "NOTICE";
            case ERROR_LOG_LEVEL:
                return "ERROR";
            case FATAL_LOG_LEVEL:
                return "FATAL";
            default:
                HSL_MSG_ASSERT(false, "invalid value of LogLevelEnum");
                return 0;
        }
    }
    
    std::string getFileLeafName(const std::string& filename)
    {
        size_t i = filename.find_last_of("\\");
        if (i == std::string::npos) return filename; // useful on linux
    else return filename.substr(i);
    }
    
    void Logger::log(LogLevelEnum l, std::ostringstream& os, const char* filename, int lineno) const
    {
        std::string fileLeafName = getFileLeafName(filename);
        std::cout << logLevelEnumToString(l) << "\t(" << fileLeafName << ": " << lineno << ")\t- " << os.str();
        os.str("");
    }
    
}

